﻿namespace HiMap.MapControls.Tools
{
    using HiMap.Geometry;
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class ToolTrack : ToolClass
    {
        private object DrawObj;
        private bool isMouseDown;
        protected internal int StaX;
        protected internal int StaY;

        public ToolTrack()
        {
            try
            {
                this.isMouseDown = false;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private Rectangle DrawRectangle(int x, int y)
        {
            Rectangle rect = new Rectangle();
            try
            {
                rect = new Rectangle(this.StaX, this.StaY, x - this.StaX, y - this.StaY);
                base.Map.DrawRectangle(new Pen(Color.Red, 1f), rect);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            return rect;
        }

        private IEnvelope GetEnvelope(Rectangle rect)
        {
            IEnvelope envelope = null;
            try
            {
                IPoint point = base.Map.PointToMapPoint(rect.X, rect.Y);
                IPoint point2 = base.Map.PointToMapPoint(rect.X + rect.Width, rect.Y + rect.Height);
                envelope = new EnvelopeClass();
                if (point.X < point2.X)
                {
                    envelope.MinX = point.X;
                    envelope.MaxX = point2.X;
                }
                else
                {
                    envelope.MinX = point2.X;
                    envelope.MaxX = point.X;
                }
                if (point.Y < point2.Y)
                {
                    envelope.MinY = point.Y;
                    envelope.MaxY = point2.Y;
                    return envelope;
                }
                envelope.MinY = point2.Y;
                envelope.MaxY = point.Y;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            return envelope;
        }

        protected override void OnMouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (base.Map.IsMapRefresh)
                {
                    this.isMouseDown = false;
                    MessageBox.Show("Loading...");
                }
                else
                {
                    this.isMouseDown = true;
                    this.DrawObj = null;
                }
                this.StaX = e.X;
                this.StaY = e.Y;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        protected override void OnMouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (this.isMouseDown && (this.StaX != e.X))
                {
                    base.Map.MapRefresh(0, 0);
                    this.DrawObj = this.DrawRectangle(e.X, e.Y);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        protected override void OnMouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                if (this.isMouseDown)
                {
                    if (this.DrawObj != null)
                    {
                        this.Stop(this.GetEnvelope((Rectangle) this.DrawObj), e.X, e.Y);
                    }
                    else
                    {
                        this.Stop(null, e.X, e.Y);
                    }
                    this.isMouseDown = false;
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        protected override void OnToolUpdated(object sender, EventArgs e)
        {
        }

        protected virtual void Stop(IGeometry geo, int x, int y)
        {
        }
    }
}

